<template>
  <div ref="chart1" :style="{ width: '100%', height: '100%' }"></div>
</template>

<script setup lang="ts">
import { ref, onMounted, getCurrentInstance } from 'vue'
// 引入接口
import { reqPieDecorationAPI } from '@/api/visual/index'

const chart1 = ref()
const { proxy } = getCurrentInstance() as any
const datas = ref([])
// 发起请求的函数
const init = async () => {
  let res = await reqPieDecorationAPI()
  if (res.code === 200) {
    datas.value = res.data.datas
  }
}
onMounted(() => {
  init().then(() => {
    // 获取挂载的组件实例
    const echarts = proxy.$ECharts
    //初始化挂载
    const echarts1 = echarts.init(chart1.value)
    //添加配置
    echarts1.setOption({
      legend: {
        data: datas.value.map((item) => item.name),
        textStyle: {
          color: '#49bcf7',
        },
      },
      tooltip: {
        trigger: 'item', // 鼠标悬停在数据项时触发 tooltip
        formatter: '{b}: {d}%', // 显示标签名和对应的百分比
      },
      series: [
        {
          name: '建筑类型占比',
          type: 'pie',
          radius: '55%',
          center: ['50%', '60%'],
          data: datas.value,
          itemStyle: {
            emphasis: {
              shadowBlur: 10,
              shadowOffsetX: 0,
              shadowColor: 'rgba(0, 0, 0, 0.5)',
            },
          },
        },
      ],
    })
    // 自适应
    window.onresize = function () {
      echarts1.resize()
    }
  })
})
</script>

<style lang="scss" scoped></style>
